package com.woniuxy.java102vuecli.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @Author author
 * @Create 2023-09-27 15:41
 */
@RestController
public class SmsController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RequestMapping("/sms/{phone}")
    public ResponseResult email(@PathVariable("phone") String phone){
        // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G3PvfsiMYdRM3oZ6dbU", "aWWdEn3fS4jqP5yv49XZv8SJTgpqRV");
        //发送短信的核心类
        IAcsClient client = new DefaultAcsClient(profile);
        //设置参数
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone);
        request.setSignName("蜗牛学院在线课堂");
        request.setTemplateCode("SMS_189611052");
        request.setTemplateParam("{\"code\":\""+ UUID.randomUUID().toString().substring(0, 4) +"\"}");

        try {
            //发送短信
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return new ResponseResult(200,"ok","短信发送成功，请查收");
    }
}
